package com.smallkingson.ylib.core.utils;

import com.smallkingson.ylib.core.Yr;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.w3c.dom.ls.LSInput;

public class YCollections {

  /**
   * @param args
   */
  public static void main(String[] args) {

  }

  /**
   * 判断一个集合里面是否有相同的元素， 注意 这里是根据equals来判断的
   */
  public static <T> boolean hasSameElement(List<T> t) {

    for (int i = 0; i < t.size(); i++) {
      for (int j = 0; j < t.size(); j++) {
        if (i == j) {
          continue;
        }
        if (t.get(i).equals(t.get(j))) {
          return true;
        }
      }
    }
    return false;
  }

  /**
   * 判断两个集合中是否有相同的元素 ，注意这里是用equals判断的
   */
  public static <T> boolean hasSameElement(List<T> list1, List<T> list2) {

    for (int i = 0; i < list1.size(); i++) {
      for (int j = 0; j < list2.size(); j++) {
        if (list1.get(i).equals(list2.get(j))) {
          return true;
        }
      }
    }
    return false;
  }

  /**
   * 给我一个list ，和一个它存放的类型，我给你把它转成数组
   */
  public static <T> Object[] ListToArray(List list, Class clazz) {
    if (list == null) {
      try {
        throw new Exception("不能为空啊");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if (list.get(0).getClass() != clazz) {
      try {
        throw new Exception("类型不一致啊");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if (clazz == String.class) {
      String[] arr = new String[list.size()];
      for (int i = 0; i < arr.length; i++) {
        arr[i] = (String) list.get(i);
      }
      return arr;
    } else if (clazz == Integer.class) {
      Integer[] arr = new Integer[list.size()];
      for (int i = 0; i < arr.length; i++) {
        arr[i] = (Integer) list.get(i);
        return arr;
      }
    }
    return null;

  }

  /**
   * @param toStred
   * @param t
   * @return
   */
  public static <T> List<T> parseList(String toStred, T t) {
    return null;
  }

  /**
   * 将List.toString 所输出的字符串 解析成list
   */
  public static List<String> parseList(String toStred) {
    List<String> list = new LinkedList<String>();
    toStred = toStred.replace("[", "").replace("]", "");
    for (String s : toStred.split(",")) {
      list.add(s.trim());
    }
    return list;
  }


  public static <T> T replace(List<T> list, int index, T newValue) {

    if (list.size() <= index) {

      Yr.logError(new Exception("下标越界"));
    }

    T remove = list.remove(index);

    list.add(index, newValue);

    return remove;
  }

  public static void intPlusPlus(List<Integer> list, int index) {
    if (list.size() <= index) {

      Yr.logError(new Exception("下标越界"));
    }

    int remove = list.remove(index);

    list.add(index, ++remove);
  }

  public static <T> List<T> filter(List<T> list, FilterHandler<T> filterHandler) {
    List<T> newList = new ArrayList<>();
    for (T t : list) {
      if (filterHandler.filter(t)) {
        newList.add(t);
      }
    }
    return newList;
  }

  public static <F, T> List<T> map(List<F> list, MapHandler<F, T> handler) {
    List<T> newList = new ArrayList<>();
    for (F f : list) {
      newList.add(handler.map(f));
    }
    return newList;
  }

  public static String join(List<String> list, String split) {
    if (list.size() == 0) {
      return "";
    }
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < list.size(); i++) {
      String s = list.get(i);
      if (i != list.size() - 1) {
        buffer.append(s).append(split);
      } else {
        buffer.append(s);
      }
    }
    return buffer.toString();
  }

  public static interface FilterHandler<T> {

    boolean filter(T t);
  }

  public static interface MapHandler<F, T> {

    T map(F t);
  }
}
